package com.base.core.util;

import com.base.core.error.ErrorUtils;

import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;


public class ProxyUtils {
    
	private static Map<Class<?>, Object> cache = new HashMap<>();

	@SuppressWarnings("unchecked")
	public static <T> T getInstance(Class<T> type) {
		return (T) cache.computeIfAbsent(type, (t) -> {
			return proxy(type);
		});
	}

	private static Object proxy(Class<?> type) {
		Object o;
		try {
			o = getAll(type.newInstance());
		} catch (Exception e) {
			throw ErrorUtils.wrap(e);
		}
		return Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] { type }, (proxy, method, args) -> {
			String name = method.getName();
			if (name.startsWith("set")) {
				put(name, args[0]);
			}
			return method.invoke(o, args);
		});
	}

	private static Object getAll(Object o) {
		return o;
	}

	private static void put(String key, Object value) {

	}
}
